#ifndef _WAKEUP_USB_PROTOCOL_H_
#define _WAKEUP_USB_PROTOCOL_H_

#define SLAVE_TO_MASTER_SYNC_BYTE 0xD5			// 0b11010101
#define MASTER_TO_SLAVE_SYNC_BYTE 0xD3			// 0b11010011
#define MASTER_FIND_NEW_SLAVE_SYNC_BYTE	0xDA	// 0b11011010

#define CMD_WEB_RESET 7
#define CMD_BEEP 0x46				// 0b01000110, manchester coded 1
#define CMD_BEEP_STOP 0x49		// 0b01001001, 2
#define CMD_QUERY 0x4A			// 0b01001010, 3
#define CMD_MULTIPLE_MASK 0x80		// 0b10000000

// find/configure new slave is done in the following command:
// preamble + default_master_ID_in_slave(0xB1B2B3) + default_slave_ID_in_slave(0xFF) + 32 word (RETLW + 32 byte) 

#define CMD_MIGRATE_SLAVE_FLASH	 0x2D		// 0b00101101

#define CMD_WRITE_SLAVE_FLASH	 0x25		// 0b00100101, 4
#define CMD_READ_SLAVE_FLASH	 0x26		// 0b00100110, 5
#define CMD_READ_SLAVE_TAG_INFO	 0x29	// 0b00101001, 6
#define CMD_ARM_MOTION_SENSOR  0x2A		// 0b00101010, 7
#define CMD_DISARM_MOTION_SENSOR  0x21	// 0b00100001
#define CMD_LISTEN_FOR_ONE_EVENT	 0x9		// 0b00001001 a blocking manager command
#define CMD_SET_SLAVE_POSTBACK_INTERVAL    0x42		//0b01000010
#define CMD_ACK    0x13							// 0b00010011

#define CMD_BEEP_FOR_DURATION		0x44			// 0b01000100
#define CMD_BEEP_UNTIL_MOVED		0x45			// 0b01000101

#define MOTION_SENSOR_OPTION_SILENT		0x2
#define MOTION_SENSOR_OPTION_BEEP_ON_MOVE		0x1
#define MOTION_SENSOR_OPTION_SEND_NEXT_READING		0x4


#define CMD_CONFIG 0x7F			// indicates begin of nonblocker manager config setting command
#define CONFIG_CMD_TIMEOUT 0x1
#define CONFIG_CMD_RADIO_ON 0x2
#define CONFIG_CMD_RADIO_OFF 0x3
#define CONFIG_CMD_ORANGE_LIGHT_ON 0x4
#define CONFIG_CMD_ORANGE_LIGHT_OFF 0x5
#define CONFIG_CMD_ORANGE_LIGHT_START_RAMP 0x6
#define CONFIG_CMD_ORANGE_LIGHT_STOP_RAMP 0x7

#define REPLY_NOT_RECEIVED 0
#define REPLY_OK 0x46								//0b01000110
#define REPLY_UNSUPPORTED 0x49				//0b01001001
#define REPLY_FLASH_READ  0x26				//0b00100110

#define EVENT_COMPASS_READING 0x4A			//
#define EVENT_COMPASS_READING_PER_REQUEST  0x4B
#define EVENT_SLAVE_POSTBACK   0x45
#define EVENT_BEEP_STOPPED  0x25

//#define EVENT_TYPE_MASK 0xF				// wants to limit event type # so upper 4 bits of event type can be used to extend #of slaves

#ifndef U8
#define U8 unsigned char
#endif
#ifndef U16
#define U16 unsigned short
#endif

#ifndef UCHAR
#define UCHAR unsigned char
#endif
#ifndef WORD
#define WORD unsigned short
#endif

typedef struct{
	UCHAR Code_Slave_ID;		// default 0xff
	UCHAR Code_Master_ID_2;	// default 0xB2
	UCHAR Code_Master_ID_1;	// default 0xB1
	UCHAR Code_ADC_Run_Interval;	// default 	0x20
	UCHAR	Code_SLAVE_TO_MASTER_SYNC_BYTE; // default SLAVE_TO_MASTER_SYNC_BYTE
	UCHAR	Code_MASTER_TO_SLAVE_SYNC_BYTE; // default MASTER_FIND_NEW_SLAVE_SYNC_BYTE
	UCHAR	Code_FREQ_1; // default 	0xA9	
	UCHAR Code_FREQ_2; // default     0xF7		;2401(0x961) for normal operation, 2551(0x9F7) for find new slave
	UCHAR Code_receive_expire1; // default 	39		; x 256us
	UCHAR	Code_PostBack_Retry; // default 	4
	UCHAR Code_DataRate; // default 		0x8
	UCHAR Code_ACTIVE_INTERVAL; // default 		0xA0     	;x16.48ms
	UCHAR Code_TX_1;
	UCHAR Code_TX_2;
	UCHAR Code_RX_1;
	UCHAR Code_RX_2;

	UCHAR Code_receive_expire_dqd;
	UCHAR Code_XTAL_Wait;
	UCHAR Code_HMC_Transmit_Retry_Wait;
	UCHAR Code_HMC_Result_Wait;
	UCHAR Code_BeepStop_Transmit_Retry;
	UCHAR Code_AFC_Config;
	UCHAR Code_C2;
	UCHAR Code_Master_Signature[9];
} Slave_Flash_Section;

typedef struct{
	UCHAR Code_Tag_Type1;
	UCHAR Code_Tag_Type2;
	UCHAR Code_Tag_Version1;
	UCHAR Code_Tag_Version2;
	UCHAR Code_Description[28];
} Slave_Tag_Info_Section;

typedef struct{
	UCHAR x_hb;
	UCHAR x_lb;
	UCHAR y_hb;
	UCHAR y_lb;
	UCHAR z_hb;
	UCHAR z_lb;
} Slave_Compass_Reading;

// size: 8+32=40
//__declspec(align(2))
typedef 
union{
	struct{
	UCHAR cmd;
	UCHAR slave_id_begin;
	UCHAR slave_id_end;
	UCHAR slave_id;
	UCHAR slave_id_begin_iterate;		// use 0xFF to indicate using target_ids.   
	UCHAR slave_id_end_iterate;			// end_iterate-begin_iterate should be less than 14 (space allocated in UsbPacket_IN)
	UCHAR num_expected_replies;
	UCHAR reserved;	
	union{
			struct {
				UCHAR cmd_args[18];
				UCHAR target_ids[14];
			} general; 
			struct {
				UCHAR count;
				UCHAR count_unit;
				UCHAR reserved[16];
				UCHAR target_ids[14];
			} slave_postback_interval;
			struct {
				UCHAR interval_count;
				UCHAR diff_threshold;
				UCHAR flags; 
				UCHAR reserved[15];
				UCHAR target_ids[14];
			} motion_sensor;
			struct{
				UCHAR beep_duration;
				UCHAR reserved[17];
				UCHAR target_ids[14];
			} beep_for_duration;
			Slave_Flash_Section flash_to_write;
			UCHAR raw[32];
		}extra;
	};
	struct{
	UCHAR cmd;		// = CMD_CONFIG			// 0x71
	UCHAR config_cmd;							// 0x72
	UCHAR tx_sync_byte;							// 0x73
	UCHAR tx_master_ids[3];						// 0x74~0x76
	UCHAR rx_master_ids[3];						// 0x77~0x79
	UCHAR rx_sync_byte;							// 0x7a
	UCHAR receive_expire1;						// 0x7b
	UCHAR receive_expire_dqd;
//	UCHAR freq_b1;	// 0xA9						// 0x7c
//	UCHAR freq_b2;	// 0x61
	WORD freq_spi_cmd;
	WORD rx_spi_cmd;			// 0x9000+P16VDI+VDI_FAST+BW_270+LNA_GAIN_0dB+RSSI_m103
	WORD tx_spi_cmd;			// 0x9850   90kHz (15kHz*(M+1)) modulation index, 0dB Pout.
	WORD datarate_spi_cmd;     // 0xC608
	UCHAR cmd_timeout;							// 0x7f
	UCHAR pwm_max;			
	UCHAR pwm_min;			
	UCHAR pwm_timer_lo;
	UCHAR pwm_timer_hi;
	UCHAR reserved;
	WORD afc_spi_cmd;
	};
} UsbPacket_OUT;



typedef struct {
	UCHAR slave_id;
	UCHAR flags;
	UCHAR adcout;
	UCHAR arssi;
} Slave_Reply;

// size = 4+4*14=60
typedef
	struct{
	UCHAR cmd;
	UCHAR num_replies;
	UCHAR num_replies_expected;
	UCHAR reserved2;
	union{
		Slave_Reply replies[14];
		struct{
			Slave_Reply reply0;
			Slave_Flash_Section flash;
		} read_flash_reply;
		struct{
			Slave_Reply reply0;
			Slave_Tag_Info_Section taginfo;
		} read_taginfo_reply;
		struct{
			UCHAR slave_id;
			UCHAR event_type;
			UCHAR adcout;
			UCHAR arssi;
			Slave_Compass_Reading compass_reading;
		} event;
	} data;
} UsbPacket_IN;


#define ACTIVE_INTERVAL 0xA0     // x16.48ms

#define RSTAT_FFIT 0x8000
#define RSTAT_POR 0x4000
#define RSTAT_FFOV 0x2000
#define RSTAT_WKUP 0x1000
#define RSTAT_EXT 0x800
#define RSTAT_LBD 0x400
#define RSTAT_FFEM 0x200
#define RSTAT_RSSI 0x100
#define RSTAT_DQD 0x80
#define RSTAT_CRL 0x40
#define RSTAT_AFCSTAB 0x20
#define RSTAT_OFFS 0x1F

#define P16VDI 0x04
#define VDI_FAST 0
#define VDI_MEDIUM 0x01
#define VDI_SLOW 0x02
#define VDI_ALWAYS 0x03

#define BW_400 0x20
#define BW_340 0x40
#define BW_270 0x60
#define BW_200 0x80
#define BW_134 0xA0
#define BW_67  0xC0
#define LNA_GAIN_0dB 0
#define LNA_GAIN_m6dB 8
#define LNA_GAIN_m14dB 0x10
#define LNA_GAIN_m20dB 0x18
#define RSSI_m103 0x0
#define RSSI_m97 0x1
#define RSSI_m91 0x2
#define RSSI_m85 0x3
#define RSSI_m79 0x4
#define RSSI_m73 0x5

#endif
